package com.driving.single.common.partner.service.impl;

import com.driving.single.common.domain.TbPartnerComment;
import com.driving.single.common.entity.PageResult;
import com.driving.single.common.mapper.TbPartnerCommentMapper;
import com.driving.single.common.partner.service.CommonPartnerCommentService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

@Service
public class CommonPartnerCommentServiceImpl implements CommonPartnerCommentService {
    @Autowired
    private TbPartnerCommentMapper tbPartnerCommentMapper;
    /**
     * 根据主键查询陪练评论
     * @param id 主键
     */
    @Override
    public TbPartnerComment selectTbPartnerCommentById(Integer id) {
        return this.tbPartnerCommentMapper.selectByPrimaryKey(id);
    }
    /**
     * 选取评分最高的评论
     * @param partnerId 陪练ID
     */
    @Override
    public TbPartnerComment selectBestTbPartnerCommentByPartnerId(Long partnerId) {
        Example example = new Example(TbPartnerComment.class);
        example.setOrderByClause("`total_score` DESC");
        example.createCriteria().andEqualTo("partnerId",partnerId);
        List<TbPartnerComment> tbCoachCommentList = this.tbPartnerCommentMapper.selectByExample(example);
        if(tbCoachCommentList != null && tbCoachCommentList.size() > 0){
            return tbCoachCommentList.get(0);
        }
        return null;
    }

    @Override
    public PageResult selectPartnerCommentByPartnerId(Long partnerId, Integer page, Integer rows) {
        PageHelper.startPage(page,rows);

        Example example = new Example(TbPartnerComment.class);
        //  评论时间排序 最新时间排序  ASC
        example.setOrderByClause("`created` DESC");
        example.createCriteria().andEqualTo("partnerId",partnerId);
        List<TbPartnerComment> list = this.tbPartnerCommentMapper.selectByExample(example);

        PageInfo<TbPartnerComment> pageInfo = new PageInfo<>(list);

        PageResult pageResult = new PageResult();
        pageResult.setResult(list);
        pageResult.setPageIndex(page);
        pageResult.setTotalSize(pageInfo.getTotal());
        pageResult.setTotalPage(pageInfo.getPages());

        return pageResult;
    }
}
